package com.xiaoshuidi.cloud.module.rooms.controller.admin.roomentrust;

import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.framework.common.pojo.PageResult;
import com.xiaoshuidi.cloud.framework.security.core.util.SecurityFrameworkUtils;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomentrust.vo.AssignPrincipalVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomentrust.vo.RoomEntrustAddUpdateVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomentrust.vo.RoomEntrustPageReqVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomentrust.vo.RoomEntrustRespVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomentrust.vo.RoomEntrustStatisticsVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomentrust.vo.RoomEntrustUpdateStatusVO;
import com.xiaoshuidi.cloud.module.rooms.controller.admin.roomentrust.vo.RoomEntrustVO;
import com.xiaoshuidi.cloud.module.rooms.enums.ApiConstants;
import com.xiaoshuidi.cloud.module.rooms.service.RoomEntrustService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

@Slf4j
@Tag(name = "管理后台 - 业主委托")
@RestController
@RequiredArgsConstructor
@RequestMapping(ApiConstants.NAME + "/roomEntrust")
public class RoomEntrustController {
    private final RoomEntrustService roomEntrustService;

    @Operation(summary = "新增业主委派")
    @PostMapping("/roomEntrust-add")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:entrust:create')")
    public CommonResult<Long> roomEntrustAdd(@RequestBody @Valid RoomEntrustAddUpdateVO roomEntrustAddUpdateVO){
        return CommonResult.success(roomEntrustService.roomEntrustAdd(roomEntrustAddUpdateVO));
    }

    @Operation(summary = "编辑业主委派")
    @PostMapping("/roomEntrust-update")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:entrust:update')")
    public CommonResult<Boolean> roomEntrustUpdate(@RequestBody @Valid RoomEntrustAddUpdateVO roomEntrustAddUpdateVO){
        return CommonResult.success(roomEntrustService.roomEntrustUpdate(roomEntrustAddUpdateVO));
    }



    @Operation(summary = "业主委派单指派委托人")
    @PostMapping("/assign-principal")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:entrust:assign:principal')")
    public CommonResult<Boolean> assignPrincipal(@RequestBody @Valid AssignPrincipalVO assignPrincipalVO){
        return CommonResult.success(roomEntrustService.assignPrincipal(assignPrincipalVO));
    }

    @Operation(summary = "变更业主委派节点状态")
    @PostMapping("/roomEntrust-status-update")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:entrust:follow:status')")
    public CommonResult<Boolean> roomEntrustStatusUpdate(@RequestBody @Valid RoomEntrustUpdateStatusVO roomEntrustUpdateStatusVO){
        return CommonResult.success(roomEntrustService.roomEntrustStatusUpdate(roomEntrustUpdateStatusVO));
    }
    @Operation(summary = "删除业主委派")
    @DeleteMapping("/roomEntrust-delete/{entrustId}")
    @Parameter(name = "entrustId", description = "委派ID")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:entrust:delete')")
    public CommonResult<Boolean> roomEntrustDelete(@PathVariable("entrustId") Long entrustId){
        return CommonResult.success(roomEntrustService.roomEntrustDelete(entrustId));
    }

    @Operation(summary = "获取委托详情")
    @GetMapping("/get-roomEntrust-byId/{entrustId}")
    @Parameter(name = "entrustId", description = "委派ID")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:entrust:query')")
    public CommonResult<RoomEntrustRespVO> getRoomEntrustById(@PathVariable("entrustId") Long entrustId){
        return CommonResult.success(roomEntrustService.getRoomEntrustById(entrustId));
    }


    @Operation(summary = "获取委托统计信息")
    @GetMapping("/roomEntrust-statistics")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:entrust:query')")
    public CommonResult<List<RoomEntrustStatisticsVO>> getRoomEntrustStatistics(){
        return CommonResult.success(roomEntrustService.getRoomEntrustStatistics());
    }


    @Operation(summary = "获取委托列表")
    @PreAuthorize("@ss.hasAnyPermissions('rooms:entrust:query')")
    @PostMapping("/roomEntrust-page")
    public CommonResult<PageResult<RoomEntrustVO>> roomEntrustPage(@RequestBody RoomEntrustPageReqVO roomEntrustPageReqVO){
        return CommonResult.success(roomEntrustService.roomEntrustPage(roomEntrustPageReqVO));
    }

}
